package com.hesicare.health.service.impl;

import com.hesicare.untils.httpUtils.HttpUtils;
import net.sf.json.JSONObject;
import com.hesicare.health.entity.SysDept;
import com.hesicare.health.mapper.SysDeptMapper;
import com.hesicare.health.service.ISysDeptService;
import com.hesicare.untils.StringUtil.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 55097
 */
@Service
public class SysDeptServiceImpl implements ISysDeptService {

    @Value("${hesiUrl}")
    private String url;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    static List<String> deptList = new ArrayList<>();

    @Override
    public SysDept getSysDeptByHisCode(String hisCode) {
        SysDept sysDept = null;
        if (StringUtil.isNotBlank(hisCode)) {
            sysDept = sysDeptMapper.getSysDeptByHisCode(hisCode);
        }
        return sysDept;
    }

    @Override
    public SysDept getSysDeptByLeader(String leader) {
        SysDept sysDept = null;
        if (StringUtil.isNotBlank(leader)) {
            sysDept = sysDeptMapper.getSysDeptByLeader(leader);
        }
        return sysDept;
    }

    @Override
    public List<SysDept> getAllDept() {
        List<SysDept> sysDeptList = null;
        sysDeptList = sysDeptMapper.getAllDept();
        return sysDeptList;
    }

    @Override
    /**
     * type 是否更新机构列表
     */
    public List<String> getLeader(boolean type) {
        if (deptList.isEmpty() || type) {
            synchronized (deptList) {
                deptList = sysDeptMapper.getLeaderAndOphoneISNotNull();
            }
        }
        return deptList;

    }

    @Override
    public List<String> getLeader() {
        return sysDeptMapper.getLeader();

    }

    @Override
    public String getToken() {
        Map login = new HashMap(2);
        login.put("loginName", "admin");
        login.put("password", "admin123");
        String content = HttpUtils.doPostWithToken(url + "/login", login);
        JSONObject object = JSONObject.fromObject(content);
        return object.get("content").toString();
    }
}
